<?php

/**
 * 无创采血点
 */
namespace backend\controllers;

use bc\controller\BackendController;
use bc\model\wc\BpointModel;
use bc\util\backend\Page;
use bc\model\wc\StaffModel;
use bc\util\Uploader;

class BpointController extends BackendController
{
	public function actionIndex()
	{
		$view = [ ];
		$model = new BpointModel ();
		
		$params = 'is_invalid = 1';
		
		$page = $this->get ( 'page', 1 );
		$model->sort = 'id DESC';
		$list = $model->getList ( $params, $page, self::PAGE_SIZE );
		
		$total = $model->getTotal ( $params );
		
		$pagination = new Page ( $total, self::PAGE_SIZE );
		
		$view ['list'] = $list;
		$view ['pagination'] = $pagination;
		
		return $this->render ( 'index', $view );
	}
	
	/**
	 */
	public function actionAdd()
	{
		
		$staffModel = new StaffModel ();
		$staffModel->columns = ['bc_wc_staff.id', 'real_name'];
		$unbindStaff = $staffModel->getStaffList ( [ 
				'hospital_id' => 0 ,
				'bc_wc_staff.is_invalid' => 1
		] );
		
		$view ['unbind_staff'] = $unbindStaff;
		
		return $this->render ( 'add', $view );
	}
	
	/**
	 */
	public function actionEdit()
	{
		$id = $this->get ( 'id' );
		if (! $id)
			$this->redirect ( '/' );
		
		$model = new BpointModel ();
		$row = $model->getRowById ( $id );
		
		if (! $row)
			$this->redirect ( '/' );
		
		$staffModel = new StaffModel ();
		$staffModel->columns = ['bc_wc_staff.id', 'real_name'];
		
		$unbindStaff = $staffModel->getStaffList ( [ 
				'hospital_id' => 0 ,
				'bc_wc_staff.is_invalid' => 1
		] );
		
		$bindStaff = $staffModel->getStaffList ( [ 
				'hospital_id' => $row ['id'] ,
				'bc_wc_staff.is_invalid' => 1
		] );
		
		$view ['unbind_staff'] = $unbindStaff;
		$view ['bind_staff'] = $bindStaff;
		
		$view ['row'] = $row;
		
		return $this->render ( 'edit', $view );
	}
	
	/**
	 * 保存
	 */
	public function actionSave()
	{
		if($this->getRequest()->isPost == true)
		{
			$model = new BpointModel ();
			$staffModel = new StaffModel ();
			
			$id = $this->post('id');
			$data['name'] = $this->post('name');
			$data['visit_remark'] = $this->post('visit_remark');
			$data['address'] = $this->post('address');
			$data['route'] = $this->post('route');
			
			$staff_ids = explode(',', $this->post('staff_id'));
			

			//采血点图片 上传
			if($_FILES['bpoint_img'])
			{
				$up = new Uploader( 'bpoint_img' , $this->getUploadConfig());
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['bpoint_img'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
					
			}
			
			//交通路线图片 上传
			if($_FILES['route_img'])
			{
				$up = new Uploader( 'route_img' , $this->getUploadConfig());
				$info = $up->getFileInfo();
				if($info['state'] === 'SUCCESS')
				{
					$data['route_img'] = str_replace(UPLOAD_PATH, '', $info['url']);
				}
			}
			
			if(!$id)//插入数据
			{
				$data['create_time'] = time();
				$data['update_time'] = time();
				$data['is_invalid'] = 1;
				$hospital_id = $model->add($data);
				
				if($staff_ids[0])
				{
					foreach ($staff_ids as $staff)
					{
						$staffModel->modify(['hospital_id' => $hospital_id], $staff);
					}
				}
			
				
			}
			else//更新数据 
			{
				$data['update_time'] = time();
				$model->modify($data, $id);
				
				$staffModel->columns = ['user_id'];
				$staffModel->batchUpdate(['hospital_id' => 0], ['hospital_id' => $id]);
				
				if($staff_ids[0])
				{
					foreach ($staff_ids as $staff)
					{
						$staffModel->modify(['hospital_id' => $id], $staff);//绑定事务
					}
				}
				
			}
			
		}
		
		$output['msg'] = '修改成功！';
		$output['url'] = '/bpoint';
		$this->forward($output);
	}
	
	
	/**
	 * 删除
	 */
	public function actionDel()
	{
		$id = $this->get('id');
		$model = new BpointModel ();
		$row = $model->getRowById($id);
		if($row)
		{
			$model->modify(['is_invalid' => 0], $id);
			
			//接触绑定
			$staffModel = new StaffModel();
			$staffModel->batchUpdate(['hospital_id' => 0], ['hospital_id' => $id]);
			
		}
		
		$output['msg'] = '删除成功！';
		$output['url'] = '/bpoint';
		$this->forward($output);
	}
	
	/**
	 * @return array
	 */
	private function getUploadConfig()
	{
		//上传文件目录
		$path = UPLOAD_PATH . 'upload/';
		//上传配置
		$config = array(
				"savePath" => $path ,             //存储文件夹
				"maxSize" => 100000 ,                   //允许的文件最大尺寸，单位KB
				"allowFiles" => array( ".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp" )  //允许的文件格式
		);
			
		//背景保存在临时目录中
		$config[ "savePath" ] = $path;
		return $config;
	}
}

